package tk.heimashuma.store.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.heimashuma.store.entity.*;
import tk.heimashuma.store.repository.AddrRepository;
import tk.heimashuma.store.repository.CommodityRepository;
import tk.heimashuma.store.repository.TransactionCommodityCarRepository;
import tk.heimashuma.store.repository.TransactionRepository;
import tk.heimashuma.store.service.PayService;

import java.math.BigDecimal;
import java.util.*;

@Service
public class PayServiceImpl implements PayService {
    @Autowired
    private TransactionRepository transactionRepository;
    @Autowired
    private CommodityRepository commodityRepository;
    @Autowired
    private AddrRepository addrRepository;
    @Autowired
    private TransactionCommodityCarRepository transactionCommodityCarRepository;
    @Override
    public Transaction findTranSaction(String t_id) {
        Transaction transaction =  transactionRepository.findAllByTId(t_id);
        return transaction;
    }

    @Override
    @Transactional
    public void save(Transaction transaction) {
         transactionRepository.save(transaction);
    }

    @Override
    public void paySuccess(String out_trade_no) {
        Transaction tranSaction = findTranSaction(out_trade_no);
        tranSaction.setState(0);
        save(tranSaction);
    }

    @Override
    public Optional<Commodity> findCommodityById(Integer cid) {
        Optional<Commodity> byId = commodityRepository.findById(cid);
        return byId;
    }

    @Override
    @Transactional
    public Transaction createOneOrder(Commodity commodity, Integer num, Users user) {
        Transaction transaction = new Transaction();
        transaction.setUser(user);
        transaction.setState(3);
        transaction.setIphoneNumber(user.getIphone());
        transaction.setTId(UUID.randomUUID().toString().replace("-",""));
        List<Commodity> commodities = new ArrayList<>();
        transaction.setCreateTime(new Date());
        commodities.add(commodity);
        transaction.setCommodityList(commodities);
        transaction.setTotalMoney(commodity.getPrice().multiply(BigDecimal.valueOf(Integer.valueOf(num))));
        TransactionCommodityCar transactionCommodityCar = new TransactionCommodityCar();
        transactionCommodityCar.setSubtotal(transaction.getTotalMoney());
        transactionCommodityCar.setCount(num);
        transactionCommodityCar.setCommodity(commodity);
        transactionCommodityCar.setTransaction(transaction);
        transactionCommodityCarRepository.save(transactionCommodityCar);
        transactionRepository.save(transaction);
        return transaction;
    }

    @Override
    @Transactional
    public void bindAddr(String tid, Integer aid) {

        Transaction allByTId = transactionRepository.findAllByTId(tid);
        Optional<Addrs> byId = addrRepository.findById(aid);
        if(byId.isPresent()){
            allByTId.setAddrs(byId.get());
        }
        transactionRepository.save(allByTId);
    }
}
